﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace CreateCatalogue
{
    class CreateXml
    {
        public string XmlFilePath { get; set; }

        public CreateXml()
        {
        }

        public CreateXml(string filePath)
        {
            XmlFilePath = filePath;
        }

        public void BuildCatalogue()
        {
            Encoding encoding = Encoding.UTF8;
            XmlTextWriter writer = new XmlTextWriter(this.XmlFilePath, encoding);
            using (writer)
            {
                writer.Formatting = Formatting.Indented;
                writer.WriteStartDocument();
                writer.WriteStartElement("catalog");
                writer.WriteAttributeString("title", "custom catalog");
                this.WriteAlbum(writer, "Empire Burlesque", "Bob Dylan", "1985", "Columbia", "10.90",
                    new List<Song>() { 
                        new Song() { Title = "Prince of Plunder", Duration = "10.5" },
                        new Song() { Title = "Queen of Rock and Roll", Duration = "10.5" },
                        new Song() { Title = "Rising Sun", Duration = "10.5" },
                    });

                this.WriteAlbum(writer, "Hide your heart", "Bonnie Tyler", "1988", "CBS Records", "9.90",
                    new List<Song>() { 
                        new Song() { Title = "Prince of Plunder", Duration = "10.5" },
                        new Song() { Title = "Queen of Rock and Roll", Duration = "10.5" },
                        new Song() { Title = "Rising Sun", Duration = "10.5" },
                    });
                writer.WriteEndElement();
                writer.WriteEndDocument();
            }
        }

        private void WriteAlbum(XmlWriter writer, string name, string artist, string year, string producer,
            string price, List<Song> songs)
        {
            writer.WriteStartElement("album");
            writer.WriteElementString("name", name);
            writer.WriteElementString("artist", artist);
            writer.WriteElementString("year", year);
            writer.WriteElementString("producer", producer);
            writer.WriteElementString("price", price);
            foreach (Song song in songs)
            {
                this.WriteSong(writer, song.Title, song.Duration);
            }
            writer.WriteEndElement();
        }

        private void WriteSong(XmlWriter writer, string title, string duration)
        {
            writer.WriteStartElement("song");
            writer.WriteElementString("title", title);
            writer.WriteElementString("duration", duration);
            writer.WriteEndElement();
        }
    }
}
